﻿using HuiZe.education.ViewModel;
using PropertyChanged;
using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public partial class LoginView : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public LoginView()
        {
            InitializeComponent();
            Loaded += LoginView_Loaded;
        }
        System.Windows.Forms.WebBrowser webBrowser ;
        private void LoginView_Loaded(object sender, RoutedEventArgs e)
        {
            webBrowser = new System.Windows.Forms.WebBrowser();
            webBrowser.ScriptErrorsSuppressed = true;

            //webBrowser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
            //webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
            webBrowser.DocumentCompleted += webBrowser1_DocumentCompleted;
            LoginViewModel loginViewModel = DataContext as LoginViewModel;
            loginViewModel.LoginView = this;
            loginViewModel.NavChanged("AccountLoginView");
        }

     
        private void canvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                try
                {
                    DragMove();
                }
                catch (System.Exception ex)
                {
                }
            }
        }
        public static Image get_Fill_image(string url)
        {
            var image = new Image();
            image.Source = new BitmapImage(new Uri(url, UriKind.Absolute));
            image.Stretch = Stretch.Fill;
            return image;
        }




        public void ShowImg(string url)
        {
            //webBrowser.ScriptErrorsSuppressed = true;
            webBrowser.Navigate(url);

        }

        private void hideMessageBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //msgDetail.Visibility = System.Windows.Visibility.Collapsed;
        }


        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            SetWebBrowserSilent(sender as System.Windows.Forms.WebBrowser, true);
        }
        private void SetWebBrowserSilent(System.Windows.Forms.WebBrowser webBrowser, bool silent)
        {
            FieldInfo fi = typeof(System.Windows.Forms.WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
            if (fi != null)
            {
                object browser = fi.GetValue(webBrowser);
                if (browser != null)
                    browser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, browser, new object[] { silent });
            }
        }

        private void Window_Error(object sender, HtmlElementErrorEventArgs e)
        {
            e.Handled= true; // 阻止其他地方继续处理

        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            try
            {
                // ((System.Windows.Forms.WebBrowser)sender).Document.Window.Error +=  new HtmlElementErrorEventHandler(Window_Error);

                if (webBrowser.Url.Query.ToString().Contains("code="))
                {
                    string query = webBrowser.Url.Query.ToString();
                    string code = query.Substring(query.IndexOf("code=")+5, query.IndexOf('&')-(query.IndexOf("code=") +5)) ;
                    //msgDetail.Visibility = Visibility.Hidden;
                    LoginViewModel loginViewModel = DataContext as LoginViewModel;
                    loginViewModel.OtherLogin(code, "Wechat");

                }

                
                foreach (HtmlElement he in webBrowser.Document.GetElementsByTagName("img"))
                {
                    string str = he.GetAttribute("className");
                    if (str == "qrcode lightBorder")
                    {
                        string qrcodeUrl = he.GetAttribute("src");
                        Image image = get_Fill_image(qrcodeUrl);
                        //QrImg.Source = image.Source;
                        //msgDetail.Visibility = Visibility.Visible;
                    }
                }

            }
            catch { }



        }



    }
}
